package stream;

import org.junit.Test;

import java.util.stream.LongStream;

public class TestForkJoin {
	
	//使用并行流计算数的累加;CPU使用率剧增
	@Test
	public void test3(){
		long start = System.currentTimeMillis();
		
		Long sum = LongStream.rangeClosed(0L, 10000000000L)  //数值越大.效率越高
							 .parallel()   //切换成并行流
							 .sum();
		System.out.println(sum);
		long end = System.currentTimeMillis();
		System.out.println("耗费的时间为: " + (end - start)); //1551
	}
	//使用传统的单线程计算数的累加
	@Test
	public void test2(){
		long start = System.currentTimeMillis();
		long sum = 0L;
		for (long i = 0L; i <= 10000000000L; i++) {
			sum += i;
		}
		System.out.println(sum);
		long end = System.currentTimeMillis();
		
		System.out.println("耗费的时间为: " + (end - start)); //3637
	}
}
